// import Vue from 'vue'
// import VueRouter from 'vue-router'
import { appRouter, pageRoutes } from './routes'
import NProgress from 'nprogress'
import { cancelRequest } from '@/api/request.class'
import 'nprogress/nprogress.css'

const isProd = process.env.NODE_ENV === 'production'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      name: 'home',
      // redirect: '/create',
      component: () => import('./views/Home.vue'),
      children: [
        ...pageRoutes
      ]
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    },
    ...appRouter,
    {
      path: '/noauth',
      name: 'noauth',
      component: () => import('@/layout/NoAuth.vue')
    },
    {
      path: '/error',
      name: 'error',
      component: () => import('@/layout/Error.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})

router.afterEach((to) => {
  cancelRequest() // 取消请求
  NProgress.done()
  if (to.fullPath && isProd) {
    // 百度统计上报
    window._hmt.push(['_setAutoPageview', false])
    window._hmt.push(['_trackPageview', to.fullPath])
  }
})

export default router
